package com.example.projectmanagement.repository.study;

import com.example.projectmanagement.model.study.UserCourseEnrollment;
import com.example.projectmanagement.model.User;
import com.example.projectmanagement.model.study.Course;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Optional;

@Repository
public interface UserCourseEnrollmentRepository extends JpaRepository<UserCourseEnrollment, Long> {
    Optional<UserCourseEnrollment> findByUserAndCourse(User user, Course course);
    List<UserCourseEnrollment> findByUser(User user);
    Page<UserCourseEnrollment> findByUser(User user, Pageable pageable);
    List<UserCourseEnrollment> findByCourse(Course course);
    Page<UserCourseEnrollment> findByCourse(Course course, Pageable pageable);
    
    @Query("SELECT COUNT(uce) FROM UserCourseEnrollment uce WHERE uce.course.id = :courseId AND uce.enrollmentStatus = 'COMPLETED'")
    Integer countCompletedEnrollmentsByCourseId(@Param("courseId") Long courseId);
    
    @Query("SELECT COUNT(uce) FROM UserCourseEnrollment uce WHERE uce.user.id = :userId AND uce.enrollmentStatus = 'COMPLETED'")
    Integer countCompletedCoursesByUserId(@Param("userId") Long userId);
}